package com.hue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hue.client.VodFeignService;
import com.hue.entity.EduChapter;
import com.hue.entity.EduCourse;
import com.hue.entity.EduCourseDescription;
import com.hue.entity.EduVideo;
import com.hue.entity.vo.CourseInfo;
import com.hue.entity.vo.CoursePublishVo;
import com.hue.mapper.EduCourseMapper;
import com.hue.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 姚凤桥
 * @since 2021-06-13
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private VodFeignService vodFeignService;

    @Override
    @Transactional
    public String add(CourseInfo courseInfo) {
        //添加课程
        EduCourse eduCourse = new EduCourse();
        //为eduCourse赋值
        BeanUtils.copyProperties(courseInfo, eduCourse);
        boolean b = save(eduCourse);
        if (!b) {
            throw new RuntimeException("添加课程失败!");
        }
        //获取添加之后的id
        String id = eduCourse.getId();
        //添加课程描述
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfo.getDescription());
        eduCourseDescription.setId(id);
        eduCourseDescriptionService.save(eduCourseDescription);
        return id;
    }

    @Override
    public CourseInfo findById(String id) {
        //查询course
        EduCourse course = getById(id);
        //查询course_description
        EduCourseDescription description = eduCourseDescriptionService.getById(id);
        //将course和description封装为CourseInfo
        CourseInfo courseInfo = new CourseInfo();
        BeanUtils.copyProperties(course, courseInfo);
        BeanUtils.copyProperties(description, courseInfo);
        return courseInfo;
    }

    @Override
    @Transactional
    public void update(CourseInfo courseInfo) {
        //创建course
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfo, eduCourse);
        //创建description
        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfo, description);
        //更新信息
        updateById(eduCourse);
        eduCourseDescriptionService.updateById(description);
    }

    @Override
    public CoursePublishVo findCoursePublishVoByCourseId(String courseId) {
        return baseMapper.findCoursePublishVoByCourseId(courseId);
    }

    @Override
    public boolean publish(String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        boolean b = updateById(eduCourse);
        return b;
    }

    /*
        删除课程:
            1.删除课程的描述信息
            2.删除课程的所有章节
            3.删除课程的所有小节以及视频
            4.删除该课程
     */
    @Override
    public boolean deleteById(String id) {
        try {
            //1.删除课程的描述信息
            eduCourseDescriptionService.removeById(id);

            //2.删除课程的所有章节信息
            QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
            chapterQueryWrapper.eq("course_id", id);
            eduChapterService.remove(chapterQueryWrapper);

            //3.删除课程的所有小节以及视频信息
            //根据courseId查询出所有的VideoId
            QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
            videoQueryWrapper.eq("course_id", id);
            List<EduVideo> eduVideos = eduVideoService.list(videoQueryWrapper);
            List<String> videoIds = new ArrayList<>();
            for (EduVideo eduVideo : eduVideos) {
                videoIds.add(eduVideo.getVideoSourceId());
            }
            //批量删除所有视频
            vodFeignService.deletes(videoIds);
            //删除课程对应的小节信息
            eduVideoService.remove(videoQueryWrapper);
            //4.删除课程
            removeById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
